const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    CDNPATH: app.globalData.CDNPATH,
    id: '',
    firstId: '',
    isNodate: false,
    isLoadMore: false,
    page: {
      currentPage: 1,
      pageSize: 10,
      total: 0
    },
    typeList: [],
    list: [],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options)
    const { id, courseLibraryName } = options
    this.setData({
      id,
      firstId: id
    })
    wx.setNavigationBarTitle({
      title: `${courseLibraryName}`
    })
    wx.hideHomeButton()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.setData({
      'page.currentPage': 1
    })
    this.getCourseTypeList()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  getCourseTypeList () {
    app.api.post({
      url: 'vx_study_subCourseLibrary_list',
      data: { id: this.data.id }
    }).then(res => {
      let arr = res
      if (arr.length > 0) {
        arr.map((v, k) => {
          v.cur = false
        })
        arr[0].cur = true
      }
      this.setData({
        typeList: arr ? arr : []
      })
      this.getProductList()
    })
  },
  toView (e) {
    console.log(e.currentTarget.id)
    wx.navigateTo({
      url: `/pages/learn/course-detail/courseDetail?id=${e.currentTarget.id}&courseLibraryId=${this.data.firstId}&from=learning`,
    })
  },
  tapType: function (e) {
    console.log(e)
    const { id } = e.currentTarget.dataset
    this.data.typeList.map((v, k) => {
      v.cur = v.id === id
    })
    this.setData({
      id,
      list: [],
      typeList: this.data.typeList,
      page: {
        currentPage: 1,
        pageSize: 10
      }
    })
    this.getProductList()
  },
  getProductList: function () {
    let { id, page, list } = this.data
    const { currentPage, pageSize } = page
    app.api.post({
      url: 'vx_study_course_list',
      data: { currentPage, pageSize, id }
    }).then(res => {
      let arr = []
      if (res.list.length > 0) {
        arr = res.list
        arr.map((v,k) => {
          v.per = (v.chapterCompleteNum/v.chapterTotal)*100
        })
        if (this.data.isLoadMore) {
          console.log('执行加载')
          list.push(...arr)
        } else {
          console.log('执行加载2')
          list = arr
        }
        this.setData({
          list,
          isLoadMore: false,
          isNodate: !(Number(res.total)),
          'page.total': Number(res.total)
        })
      }
    })
  },
  loadMore: function () {
    const { currentPage, total } = this.data.page
    if (total > this.data.list.length) {
      this.setData({
        'page.currentPage': currentPage + 1,
        'isLoadMore': true
      })
      this.getProductList()
    }
  },
})